package com.liu.voluntary.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.voluntary.mapper.AdminMapper;
import com.liu.voluntary.pojo.Admin;
import com.liu.voluntary.pojo.User;
import com.liu.voluntary.service.AdminService;
import com.liu.voluntary.service.UserService;
import com.liu.voluntary.util.CookieUtil;
import com.liu.voluntary.util.MD5Util;
import com.liu.voluntary.util.UUIDUtil;
import com.liu.voluntary.util.ValidatorUtil;
import com.liu.voluntary.vo.RespBean;
import com.liu.voluntary.vo.RespBeanEnum;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Resource
    private UserService userService;

    @Resource
    private AdminService adminService;
    @Resource
    private AdminMapper adminMapper;

    @RequestMapping("/toLoginPage")
    public String toLoginPage() {

        return "login";
    }


    @RequestMapping("/toUser")
    public String toUser(@RequestParam("l_mobile") String mobile,
                        @RequestParam("l_password") String password,
                        HttpServletRequest request, HttpServletResponse response, Model model) {

        RespBean userBean = userService.login(mobile, password, request, response);
        if (userBean.getCode() != 200) {
            model.addAttribute("msg", userBean.getMessage());
            return "login";
        }

        String ticket = (String) userBean.getObj();
        User user = userService.getUserInRedis(ticket, request, response);

        if (user == null) {
            return "login";
        }

        // 将用户信息添加到model中
        model.addAttribute("user", user);
        model.addAttribute("mobile", user.getMobile());

        return "user";
    }




    @RequestMapping("/toAdmin")

    public String  toAdmin(@RequestParam("l_mobile") String mobile,
                           @RequestParam("l_password") String password,
                           HttpServletRequest request, HttpServletResponse response,
                           Model model) {



        RespBean adminBean = adminService.login(mobile, password, request, response);

        if (adminBean.getCode() != 200){
            model.addAttribute("msg",adminBean.getMessage());
            return "login";
        }

        String ticket =(String) adminBean.getObj();

        Admin admin = adminService.getAdminInRedis(ticket, request, response);

//        String ticket = CookieUtil.getCookieValue(request, "adminTicket");

//        Admin admin =(Admin) request.getSession().getAttribute(ticket);

        if (admin == null){
            return "login";
        }

        if (admin.getRole().equals("superAdmin")){
            return "superAdmin";
        }


        return "admin";

    }


    @RequestMapping("/toAdminPage")

    public String  toAdmin() {

        return "admin";
    }


    @RequestMapping("/toUserPage")
    public String toUser(@CookieValue("Ticket") String Ticket, Model model) {
        // 从Redis获取用户信息
        User user = userService.getUserInRedis(Ticket, null, null);
        if (user != null) {
            model.addAttribute("user", user);
            model.addAttribute("mobile", user.getMobile());
        }
        return "user";
    }

  @RequestMapping("/toSuperAdminPage")

    public String  toSuperAdmin() {

        return "superAdmin";
    }







}
